package com.example.fitnessservice.service.impl;

import com.example.fitnessservice.module.dao.bbsmapper.BbsMapper;
import com.example.fitnessservice.module.pojo.*;
import com.example.fitnessservice.service.BbsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class BbsServiceImpl implements BbsService {
    @Resource
    private BbsMapper bbsMapper;
    @Override
    public List<UserDynamic> recommendDynamic() {
        List<UserDynamic> recommendDynamic = bbsMapper.findDynamic();
        Collections.sort(recommendDynamic, new Comparator<UserDynamic>() {
            @Override
            public int compare(UserDynamic o1, UserDynamic o2) {
                return o2.getDynamicLike() - o1.getDynamicLike();
            }
        });
        return recommendDynamic;
    }

    @Override
    public UserDynamic dynamicContent(Integer dynamicId) {
        List<UserDynamic> recommendDynamic = this.recommendDynamic();
        for (UserDynamic userDynamic : recommendDynamic) {
            if (userDynamic.getDynamicId() == dynamicId){
                return userDynamic;
            }
        }
        return null ;
    }

    @Override
    public UserInfo findUserInfo(Integer userId) {
        UserInfo userInfo = bbsMapper.findUserInfo(userId);
        return userInfo;
    }

    @Override
    public List<UserDynamicFans> findAllFans(Integer userId) {
        List<UserDynamicFans> allFans = bbsMapper.findAllFans(userId);
        return allFans;
    }

    @Override
    public List<UserDynamicAttention> findAllAttention(Integer userId) {
        List<UserDynamicAttention> allAttention = bbsMapper.findAllAttention(userId);
        return allAttention;
    }

    @Override
    public int findFansNum(Integer userId) {
        int count = bbsMapper.findFansNum(userId);
        return count;
    }

    @Override
    public int findAttentionNum(Integer userId) {
        int count = bbsMapper.findAttentionNum(userId);
        return count;
    }

    @Transactional
    @Override
    public boolean addAttention(UserDynamicAttention userDynamicAttention) {
        int count1 = bbsMapper.addAttention(userDynamicAttention);
        UserDynamicFans userDynamicFans = new UserDynamicFans();
        userDynamicFans.setUserId(userDynamicAttention.getAttentionId());
        userDynamicFans.setFansId(userDynamicAttention.getUserId());
        int count2 = bbsMapper.addFans(userDynamicFans);
        if (count1 < 1 || count2 < 1){
            throw new RuntimeException("添加关注失败,请返回重试");

        }
        return true;
    }

    @Transactional
    @Override
    public boolean deleteAttention(UserDynamicAttention userDynamicAttention) {
        int count1 = bbsMapper.deleteAttention(userDynamicAttention);
        UserDynamicFans userDynamicFans = new UserDynamicFans();
        userDynamicFans.setUserId(userDynamicAttention.getAttentionId());
        userDynamicFans.setFansId(userDynamicAttention.getUserId());
        int count2 = bbsMapper.deleteFans(userDynamicFans);
        if (count1 < 1 || count2 < 1){
            throw new RuntimeException("取消关注失败,请返回重试");

        }
        return true;
    }

    @Override
    public List<UserDynamic> findDynamicByUserId(Integer userId) {
        List<UserDynamic> userDynamics = bbsMapper.findDynamicByUserId(userId);
        Collections.reverse(userDynamics);
        return userDynamics;
    }

    @Override
    public boolean plusLike(Integer dynamicId) {
        int count = bbsMapper.plusLike(dynamicId);
        return count < 1 ? false : true;
    }

    @Override
    public boolean subtractLike(Integer dynamicId) {
        int count = bbsMapper.subtractLike(dynamicId);
        return count < 1 ? false : true;
    }

    @Override
    public boolean addDynamicComment(UserDynamicComment userDynamicComment) {
        List<UserDynamicSensitiveWord> sensitiveWords = bbsMapper.findSensitiveWords();
        for (UserDynamicSensitiveWord sensitiveWord : sensitiveWords) {
            String str = userDynamicComment.getCommentContent();
            String word = sensitiveWord.getSensitiveWord();
            int i = str.indexOf(word);
            if (i != -1){
                return false;
            }
        }
        int count = bbsMapper.addDynamicComment(userDynamicComment);
        return true;
    }

    @Override
    public boolean deleteDynamicComment(UserDynamicComment userDynamicComment) {
        int count = bbsMapper.deleteDynamicComment(userDynamicComment);
        return count < 1 ? false : true;
    }

    @Override
    public List<UserDynamicComment> findDynamicComment(Integer dynamicId) {
        List<UserDynamicComment> userDynamicComments = bbsMapper.findDynamicComment(dynamicId);
        return userDynamicComments;
    }

    @Override
    public List<UserDynamicReport> findReportContent() {
        List<UserDynamicReport> userDynamicReport = bbsMapper.findReportContent();
        return userDynamicReport;
    }

    @Override
    public boolean submitReport(Integer reportId,Integer dynamicId) {
        int count = bbsMapper.submitReport(reportId,dynamicId);
        return count < 1 ? false : true;
    }

    @Override
    public List<UserDynamic> findAttentionDynamic(Integer userId) {

        List<UserDynamicAttention> allAttention = this.findAllAttention(userId);
        List<UserDynamic> allAttentionDynamics = new ArrayList<>();
        for (UserDynamicAttention attention : allAttention) {
            Integer attentionId = attention.getAttentionId();
            List<UserDynamic> attentionDynamics = this.findDynamicByUserId(attentionId);
            for (UserDynamic attentionDynamic : attentionDynamics) {
                allAttentionDynamics.add(attentionDynamic);
            }
        }
        return allAttentionDynamics;
    }

    @Override
    public int dynamicIssue(UserDynamicContent userDynamicContent) {
        return 0;
    }

    @Override
    public boolean deleteDynamic(Integer dynamicId) {
        bbsMapper.deleteDynamicCommentByDynamicId(dynamicId);
        bbsMapper.deleteDynamicContent(dynamicId);
        int count = bbsMapper.deleteDynamic(dynamicId);
        return count < 1 ? false : true;
    }

    @Override
    public boolean addUserInfo(UserInfo userInfo) {
        UserInfo userInfo1 = this.findUserInfo(userInfo.getUserId());
        int count = 0;
        if (userInfo1 == null){
            count = bbsMapper.addUserInfo(userInfo);
        } else {
            count = bbsMapper.updateUserInfo(userInfo);
        }
        return count < 1 ? false : true;
    }

    @Override
    public List<String> findAttentionDynamicImg(Integer userId) {
        List<UserDynamicAttention> allAttention = this.findAllAttention(userId);
        List<String> findAttentionDynamicImg = new ArrayList<>();
        for (UserDynamicAttention attention : allAttention) {
            Integer attentionId = attention.getAttentionId();
            List<UserDynamic> attentionDynamics = this.findDynamicByUserId(attentionId);
            for (UserDynamic attentionDynamic : attentionDynamics) {
                findAttentionDynamicImg.add(attentionDynamic.getUserDynamicContent().getImg());
            }
        }
        return findAttentionDynamicImg;
    }
}
